<template>
  <div class="loginPage">
    <div class="loginBox">
      <h2 class="center">智能管理后台</h2>
      <el-form :model="form" ref="ruleFormRef" :rules="rules" label-width="80px" label-position="left" style="display: flex; justify-content: center; flex-wrap: wrap">
        <el-form-item label="用户名:" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名" />
        </el-form-item>
        <div style="width: 100%; height: 20px"></div>
        <el-form-item label="密码:" prop="pwd">
          <el-input v-model="form.pwd" show-password type="password" placeholder="请输入密码" />
        </el-form-item>
        <div style="width: 100%; height: 30px"></div>
        <el-button type="primary" @click="onSubmit(ruleFormRef)" style="width: 80%">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import req from '@/axios/index'
import { useUser } from '@/store/user.js'
localStorage.clear()
const userStore = useUser()
userStore.changeCurrentUser({})
const ruleFormRef = ref()
const router = useRouter()
const form = reactive({
  name: 'admin',
  pwd: 'admin'
})
const rules = reactive({
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 5, message: '用户名长度为3-5位', trigger: 'blur' }
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 5, message: '密码长度为3-5位', trigger: 'blur' }
  ]
})
const onSubmit = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      const data={
        name:form.name,
        password:form.pwd,
      }
      // req.post(`/login`,data).then(res=>{
      //   userStore.changeCurrentUser(res)
      //   localStorage.setItem('token',res.token)
      //   router.push('/home')
      // })
      const res={
        name:'admin',
        roleName:'超级管理员',
        avatar:'http://minio.can.anchnet.com/coding-static-bucket-public/WM-TEXT-AVATAR-wSJopVLGNYYMxqLoMyEe.jpg',
        token:'123'
      }
      userStore.changeCurrentUser(res)
      localStorage.setItem('token',res.token)
      router.push('/home')
    } else {
      console.log('error submit!')
      return false
    }
  })
}
</script>

<style scoped>
.loginPage {
  width: 100%;
  height: 100%;
  background-image: url(../../assets/images/loginBg.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}
.loginBox {
  width: 500px;
  box-shadow: rgba(0, 0, 0, 0.12) 0px 0px 6px 0px;
  border-radius: 5px;
  background-color: rgba(255, 255, 255, 0.77);
  padding: 20px;
}
:deep(.el-input__wrapper) {
  background-color: transparent;
}
:deep(.el-form-item) {
  width: 80%;
}
</style>
